cat("\f")
rm(list=ls())
options(scipen = 999)

library(dplyr)  #adattáblákkal való műveletekhez
library(ggplot2) #vizualizációkhoz
library(writexl) #xlsx-be kiírása táblázatoknak
library(extrafont) #betűtípusok betöltése
font_import()
loadfonts(device = "win")

set.seed(123)

#betöltöm a szükséges fájlokat:

ctable <- readRDS("data/ctable.RDS")
df <- readRDS("data/full_df3.RDS")

names(ctable)[names(ctable) == "id"] <- "ctable_nearest"

df <- left_join(df, select(ctable, c(ctable_nearest, color, secondary_hexcode, primary_hexcode)),by = join_by(ctable_nearest))

names(df)

df <- df %>% select(-c(km, cluster_hex_code, wdist))

names(ctable)[names(ctable) == "ctable_nearest"] <- "id"

##########################################
#Tábla arányok a 10 legközelebbi színnel:#
##########################################

for (i in 5:20) {

ctable <- readRDS("data/ctable.RDS")

km <- readRDS(paste0("data/km/km",i,".RDS"))


df$km <- km$cluster


color_summary <- df %>% group_by(km) %>% summarise(R = mean(R),
                                                   G = mean(G),
                                                   B = mean(B))

for (j in 1:nrow(ctable)) {
  
  eval(parse(text = paste0(
    "color_summary <- color_summary %>% mutate(", ctable$id[j], "= (R - unlist(ctable[j,'R']))**2
                      + (G - unlist(ctable[j, 'G']))**2
                      + (B - unlist(ctable[j,'B']))**2)"
    
  )))
}

df_dists <- color_summary[,which(names(color_summary) %in% ctable$id)] %>% as.matrix()

color_summary <- color_summary[,which(names(color_summary) %in% ctable$id == F)]

color_summary$ctable <- apply(df_dists, 1, function(row) {
  colnames(df_dists)[which.min(row)]
})

names(ctable)[names(ctable) == "id"] <- "ctable"

color_summary <- left_join(color_summary, select(ctable, c(ctable, color)), by = join_by(ctable))

color_summary <- color_summary %>% select(-c(R,G,B,ctable))

names(color_summary)[2] <- "km_color"

count_summary <- summarise(group_by(df, km), arány = n()/nrow(df))

color_summary <- inner_join(color_summary, count_summary, by = join_by(km))

write_xlsx(color_summary, paste0("data/closest_10/km",i,"_10closest.xlsx"))

print(i)

}

#####################
#össsszevont számok:#
#####################


ctable <- readRDS("data/ctable.RDS")

names(df)

km <- readRDS("data/km/km14.RDS")


df$km <- km$cluster


df$km[df$km %in% c("5", "12", "10")] <- "5-12-10"

df$km[df$km %in% c("3", "14", "13")] <- "3-14-13"

df$km[df$km %in% c("6", "9", "4", "11")] <- "6-9-4-11"

df$km[df$km %in% c("7", "8")] <- "7-8"



color_summary <- df %>% group_by(km) %>% summarise(R = mean(R),
                                                   G = mean(G),
                                                   B = mean(B))


color_summary <- color_summary %>% mutate(cluster_hex_code = rgb(red = R,
                                                                 green = G,
                                                                 blue = B,
                                                                 maxColorValue = 255))


df <- left_join(df, select(color_summary, c(km, cluster_hex_code)), by = join_by(km))



for (i in 1:nrow(ctable)) {
  
  eval(parse(text = paste0(
    "color_summary <- color_summary %>% mutate(", ctable$id[i], "= (R - unlist(ctable[i,'R']))**2
                      + (G - unlist(ctable[i, 'G']))**2
                      + (B - unlist(ctable[i,'B']))**2)"
    
  )))
  print(i)
}


df_dists <- color_summary[,which(names(color_summary) %in% ctable$id)] %>% as.matrix()

color_summary <- color_summary[,which(names(color_summary) %in% ctable$id == F)]

color_summary$ctable <- apply(df_dists, 1, function(row) {
  colnames(df_dists)[which.min(row)]
})

names(ctable)[names(ctable) == "id"] <- "ctable"

color_summary <- left_join(color_summary, select(ctable, c(ctable, color)), by = join_by(ctable))

color_summary <- color_summary %>% select(-c(R,G,B,ctable, cluster_hex_code))

names(color_summary)[2] <- "color2"

df <- left_join(df, color_summary, by = join_by(km))

names(df)

sum(df$color == df$color2)/nrow(df) #0.601976


write_xlsx(color_summary, "data/closest_10/kmösszevont_10closest.xlsx")
